package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.input.SmsHomeAdvertiseInpDTO;
import com.cloud.mall.sms.input.SmsHomeBrandInpDTO;
import com.cloud.mall.sms.mapper.SmsHomeAdvertiseMapper;
import com.cloud.mall.sms.mapper.SmsHomeBrandMapper;
import com.cloud.mall.sms.model.SmsHomeAdvertise;
import com.cloud.mall.sms.model.SmsHomeAdvertiseExample;
import com.cloud.mall.sms.model.SmsHomeBrand;
import com.cloud.mall.sms.model.SmsHomeBrandExample;
import com.cloud.mall.sms.out.SmsHomeAdvertiseOut;
import com.cloud.mall.sms.out.SmsHomeBrandOut;
import com.cloud.mall.sms.service.SmsHomeAdvertiseService;
import com.cloud.mall.sms.service.SmsHomeBrandService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class SmsHomeBrandServiceImpl implements SmsHomeBrandService {
    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody List<SmsHomeBrandInpDTO> homeBrandList) {
        List<SmsHomeBrand> brandList=MallBeanUtils.copyDoToDto(homeBrandList,SmsHomeBrand.class);
        for (SmsHomeBrand smsHomeBrand : brandList) {
            smsHomeBrand.setRecommendStatus(1);
            smsHomeBrand.setSort(0);
            homeBrandMapper.insert(smsHomeBrand);
        }
        return BaseResponse.setResultSuccess(brandList.size());
    }

    @Override
    public BaseResponse updateRecommendStatus(List<Long> ids,Integer recommendStatus) {
        SmsHomeBrandExample example = new SmsHomeBrandExample();
        example.createCriteria().andIdIn(ids);
        SmsHomeBrand record = new SmsHomeBrand();
        record.setRecommendStatus(recommendStatus);
        int count=homeBrandMapper.updateByExampleSelective(record,example);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Transactional
    @Override
    public BaseResponse delete(@RequestBody List<Long> ids) {
        SmsHomeBrandExample example = new SmsHomeBrandExample();
        example.createCriteria().andIdIn(ids);
        return BaseResponse.setResultSuccess(homeBrandMapper.deleteByExample(example));
    }

    @Override
    public BaseResponse update(Long id, Integer sort) {
        SmsHomeBrand homeBrand = new SmsHomeBrand();
        homeBrand.setId(id);
        homeBrand.setSort(sort);
        int count= homeBrandMapper.updateByPrimaryKeySelective(homeBrand);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }


    @Override
    public BaseResponse<CommonPage<SmsHomeBrandOut>> list(String brandName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SmsHomeBrandExample example = new SmsHomeBrandExample();
        SmsHomeBrandExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(brandName)){
            criteria.andBrandNameLike("%"+brandName+"%");
        }
        if(recommendStatus!=null){
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        example.setOrderByClause("sort desc");
        List<SmsHomeBrand> homeBrandList =homeBrandMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(homeBrandList, SmsHomeBrandOut.class)));
    }


}
